package com.cskaoyan.market.service;

import com.cskaoyan.market.db.domain.MarketGoodsExample;
import com.cskaoyan.market.db.domain.MarketGoodsProductExample;
import com.cskaoyan.market.db.domain.MarketOrderExample;
import com.cskaoyan.market.db.domain.MarketUserExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketGoodsProductMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.util.MybatisUtils;
import com.cskaoyan.market.vo.MarketDashboardVo;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletContext;

public class AdminDashboardServiceImpl implements AdminDashboardService {

    // private final HttpServletRequest req;
    // private final HttpServletResponse resp;
    //
    // public AdminDashboardServiceImpl(final HttpServletRequest req, final HttpServletResponse resp) {
    //     this.req = req;
    //     this.resp = resp;
    // }

    @Override
    public MarketDashboardVo dashboard() {
        SqlSession sqlSession = MybatisUtils.getSession();
        MarketGoodsMapper marketGoodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
        MarketUserMapper marketUserMapper = sqlSession.getMapper(MarketUserMapper.class);
        MarketGoodsProductMapper productMapper = sqlSession.getMapper(MarketGoodsProductMapper.class);
        MarketOrderMapper marketOrderMapper = sqlSession.getMapper(MarketOrderMapper.class);

        //context域
        // ServletContext context = req.getServletContext();


        MarketGoodsExample marketGoodsExample = new MarketGoodsExample();
        Long goodsTotal = marketGoodsMapper.countByExample(marketGoodsExample);
        // context.setAttribute("goodsTotal", goodsTotal);

        MarketUserExample marketUserExample = new MarketUserExample();
        Long userTotal = marketUserMapper.countByExample(marketUserExample);
        // context.setAttribute("userTotal", userTotal);

        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        Long productTotal = productMapper.countByExample(productExample);
        // context.setAttribute("productTotal", productTotal);

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        Long orderTotal = marketOrderMapper.countByExample(marketOrderExample);
        // context.setAttribute("orderTotal", orderTotal);

        MarketDashboardVo marketDashboardVo = new MarketDashboardVo(goodsTotal, userTotal, productTotal, orderTotal);
        sqlSession.commit();
        sqlSession.close();
        return marketDashboardVo;
    }
}
